<template>
    <div class="home-content">
        <div class="product-grid">
            <div v-for="product in products" :key="product.id" class="product-card" @click="viewProduct(product)">
                <img :src="product.imageUrl" :alt="product.name" class="product-image" />
                <div class="info">
                    <h3 class="product-name">{{ product.name }}</h3>
                    <div class="row">
                        <p class="product-price">价格: {{ product.price | currency }}</p>
                        <p class="product-sales">销售量: {{ product.sales }}</p>
                    </div>
                </div>
            </div>
        </div>
        <el-pagination v-model:currentPage="currentPage" :page-size="itemsPerPage" :total="total"
            @current-change="fetchProducts" />
    </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { get } from "../until/request";
import 'element-plus/dist/index.css'; // 引入 Element Plus 样式
import { ElPagination } from 'element-plus';

const router = useRouter();

const products = ref([]);     // 商品列表
const total = ref(0);        // 总商品数  
const currentPage = ref(1); // 当前页码
const itemsPerPage = 8;   // 每页显示的商品数量

// 获取商品列表
async function fetchProducts(page = 1) {
    currentPage.value = page;
    const res = await get(`/api/getProductList?page=${page}&size=${itemsPerPage}`);
    products.value = res.data.list;
    total.value = res.data.total;
    console.log(res);
}

// 查看商品详情
function viewProduct(product) {
    // 将产品信息存储到 localStorage
    localStorage.setItem('selectedProduct', JSON.stringify(product));
    router.push({ name: 'ProductDetail' });
}

fetchProducts();  // 初始加载
</script>

<style lang="scss" scoped>
.home-content {
    padding: 1rem;

    .product-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        /* 一行4个商品 */
        gap: 1rem;
        padding: 1rem;

        .product-card {
            background-color: #fff;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            text-align: left;
            padding: 1rem;
            cursor: pointer;

            .product-image {
                width: 100%;
                height: 200px;
                object-fit: cover;
                border-radius: 4px;
            }

            .info {
                display: flex;
                flex-direction: column;
                justify-content: space-between;

                .product-name {
                    width: 100%;
                    height: 48px;
                    display: block;
                    // white-space: nowrap;
                    overflow: hidden;
                    text-overflow: ellipsis;
                    margin: 0.5rem 0;
                    color: #333;
                }

                .row {
                    display: flex;
                    justify-content: space-between;

                    .product-price {
                        color: #007bff;
                        font-weight: bold;
                    }

                    .product-sales {
                        color: #555;
                    }
                }
            }
        }
    }

    .pagination {
        width: 100%;
        height: 50px;
        display: flex;
        color: #333;
        justify-content: center;
        margin-top: 1rem;
        // background-color: #000;
    }

}
</style>